//
//  STComposeBottomToolBar.swift
//  STVVeibo
//
//  Created by leergou on 16/7/27.
//  Copyright © 2016年 WhiteHouse. All rights reserved.
//

import UIKit

// 按钮的枚举
enum STComposeBottomToolBarViewType: Int{
    // 图片
    case Picture = 1001
    // @
    case Mention = 1002
    // #
    case Trend = 1003
    // 表情
    case Emotion = 1004
    // +
    case Add = 1005
}

class STComposeBottomToolBar: UIView {
    // 表情按钮
    var emoticonButton: UIButton?
    // 定义一个属性,记录textView 当前键盘是否是 自定义表情键盘
    var isEmoticonKeyboard: Bool = false {
        didSet{
            // 在这里修改 表情按钮的图片
//            print("修改表情按钮的图片")
            let imageName = isEmoticonKeyboard ? "compose_keyboardbutton_background" : "compose_emoticonbutton_background"
            emoticonButton?.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
            emoticonButton?.setImage(UIImage(named: "\(imageName)_hightlighted"), forState: UIControlState.Highlighted)
        }
    }
    
    var closure:((type: STComposeBottomToolBarViewType)->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 按钮点击事件
    func buttonClick(sender: UIButton){
        // 执行闭包
        closure?(type: STComposeBottomToolBarViewType(rawValue: sender.tag)!)
    }
    
    // 设置视图
    func setupUI(){
        // 添加子控件
        let cameraButton = addChildButton(.Picture, imgName: "compose_camerabutton_background", target: self, action: "buttonClick:")
        let mentionButton = addChildButton(.Mention, imgName: "compose_mentionbutton_background", target: self, action: "buttonClick:")
        let trendButton = addChildButton(.Trend, imgName: "compose_trendbutton_background", target: self, action: "buttonClick:")
        emoticonButton = addChildButton(.Emotion, imgName:"compose_emoticonbutton_background", target: self, action: "buttonClick:")
        let addButton = addChildButton(.Add, imgName:"compose_add_background", target: self, action: "buttonClick:")
        
        // 添加约束
        cameraButton.snp_makeConstraints { (make) -> Void in
            make.leading.top.bottom.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        mentionButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(cameraButton.snp_trailing)
            make.top.bottom.equalTo(self)
            make.width.equalTo(trendButton)
        }
        trendButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(mentionButton.snp_trailing)
            make.top.bottom.equalTo(self)
            make.width.equalTo(emoticonButton!)
        }
        emoticonButton!.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(trendButton.snp_trailing)
            make.top.bottom.equalTo(self)
            make.width.equalTo(addButton)
        }
        addButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(emoticonButton!.snp_trailing)
            make.top.bottom.equalTo(self)
            make.trailing.equalTo(self)
        }
    }
    
    // 创建按钮方法
    func addChildButton (type:STComposeBottomToolBarViewType, imgName: String, target: AnyObject?, action: Selector?) -> UIButton{
        let button = UIButton(title: nil, titleColro: nil, font: 0, target: target, action: action)
        button.tag = type.rawValue
        button.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imgName)_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        addSubview(button)
        return button
    }
}
